const app = getApp();
const utils = require("../../utils/util.js");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    deviceList: [{
      appDeviceType: 2,
      deviceUUId: "",
      macAddr: "",
      deviceName: "掌越血脂血糖仪",
      deviceType: "XZXTY",
      medicalCodes: "TG,CHOL,HDL-C,LDL-C,TC/HDL-C,GLU,NON-HDL-C,LDL-C/HDL-C",
      medicalNames: "血糖,高密度脂蛋白胆固醇,低密度脂蛋白胆固醇,甘油三脂,总胆固醇,总胆/高密比值,非高密度脂蛋白胆固醇,低密高密比值",
      bluetoothPrefix: "SLX",
      link: '/pages/connectDetail/zhangyue/index',
      networkingMode: "NZLY",
      appIconUrl: "http://img.tmqyt.com/images/9440/15828120231330.png",
      isBind: false
    }, {
      appDeviceType: 26,
      deviceUUId: "",
      macAddr: "",
      deviceName: "UG-11系列血糖尿酸仪",
      deviceType: "XTNSY",
      medicalCodes: "GLU,UA",
      medicalNames: "血糖,血尿酸",
      bluetoothPrefix: "BDE_WEIXIN_TTM,UG11Air",
      networkingMode: "NZLY",
      link: '/pages/connectDetail/ug11/index',
      appIconUrl: "http://img.tmqyt.com/images/174/15828119051330.png",
      isBind: false
    }, {
      appDeviceType: 25,
      deviceUUId: "",
      macAddr: "",
      deviceName: "安稳+Air血糖仪",
      deviceType: "XTY",
      medicalCodes: "GLU",
      medicalNames: "血糖",
      link: '/pages/connectDetail/aw/index',
      bluetoothPrefix: "BDE_WEIXIN_TTM",
      networkingMode: "NZLY",
      appIconUrl: "http://img.tmqyt.com/images/503/15828118651330.png",
      isBind: false
    }, {
      appDeviceType: 1002,
      deviceUUId: "",
      deviceName: "金稳+Air血糖仪",
      deviceType: "XTY",
      medicalCodes: "GLU",
      medicalNames: "血糖",
      bluetoothperfix: "JW+AIR",
      link: '/pages/connectDetail/jw/index',
      networkingMode: "WZLY",
      appIconUrl: "http://img.tmqyt.com/images/9612/15828138201330.png",
      isBind: false,
    }, {
      appDeviceType: 2003,
      deviceUUId: "",
      deviceName: "真睿Air血糖仪",
      deviceType: "XTY",
      medicalCodes: "GLU",
      link: '/pages/connectDetail/zhenrui/index',
      medicalNames: "血糖",
      bluetoothPrefix: "TMX",
      networkingMode: "WZLY",
      appIconUrl: "http://img.tmqyt.com/images/9612/15828138201330.png",
      isBind: false
    }, {
      appDeviceType: 13,
      deviceUUId: "",
      macAddr: "",
      deviceName: "诺凡血压计",
      deviceType: "XYJ",
      medicalCodes: "BP,P",
      medicalNames: "脉搏,血压",
      bluetoothPrefix: "ClinkBlood",
      link: '/pages/connectDetail/nuofan/index',
      networkingMode: "NZLY",
      appIconUrl: "http://img.tmqyt.com/images/7816/15828124491330.png",
      isBind: false
    }, {
      appDeviceType: 2002,
      deviceUUId: "",
      deviceName: "乐福体脂秤",
      bluetoothperfix: "Weight",
      link: '/pages/connectDetail/lefu/index',
      networkingMode: "NZLY",
      appIconUrl: "http://img.tmqyt.com/images/9440/15828120231330.png",
      isBind: false,
    },
    {
      appDeviceType: 2001,
      deviceUUId: "",
      deviceName: "hpalc新平台(GM501、UA Ring等)",
      deviceType: "XTY",
      medicalCodes: "GLU",
      medicalNames: "血糖",
      bluetoothperfix: "SN-",
      link: '/pages/connectDetail/hpalc/index',
      networkingMode: "NZLY",
      appIconUrl: "http://img.tmqyt.com/images/503/15828118651330.png",
      isBind: false
    }, {
      appDeviceType: 10,
      deviceUUId: "",
      macAddr: "",
      deviceName: "WL-1型蓝牙血糖仪",
      deviceType: "XTY",
      medicalCodes: "GLU",
      medicalNames: "血糖",
      link: '/pages/connectDetail/wl1/index',
      bluetoothPrefix: "Sinocare",
      networkingMode: "NZLY",
      appIconUrl: "http://img.tmqyt.com/images/898/15828118171330.png",
      isBind: false
    }, 
    // {
    //   appDeviceType: 44,
    //   deviceUUId: "",
    //   deviceName: "WL-1型教育专员版血糖仪",
    //   deviceType: "XTY",
    //   medicalCodes: "GLU",
    //   medicalNames: "血糖",
    //   bluetoothperfix: "Sinocare",
    //   link: '/pages/connectDetail/wl1_wechat/index',
    //   networkingMode: "NZLY",
    //   appIconUrl: "http://img.tmqyt.com/images/898/15828118171330.png",
    //   isBind: false,
    // },
    // {
    //   appDeviceType: 3,
    //   deviceUUId: "",
    //   macAddr: "",
    //   deviceName: "EA-12血糖尿酸测试仪",
    //   deviceType: "XTNSY",
    //   medicalCodes: "GLU,UA,U-KET",
    //   medicalNames: "血糖,尿酸,血酮",
    //   link: '/pages/connectDetail/ea12/index',
    //   bluetoothPrefix: "BDE_WEIXIN_TTM,SNPB,Jin",
    //   networkingMode: "WZLY",
    //   appIconUrl: "http://img.tmqyt.com/images/3771/15828137991330.png",
    //   isBind: false
    // }, {
    //   appDeviceType: 12,
    //   deviceUUId: "",
    //   macAddr: "",
    //   deviceName: "EA-18血糖尿酸测试仪",
    //   deviceType: "XTNSY",
    //   medicalCodes: "GLU,UA",
    //   medicalNames: "血糖,尿酸",
    //   link: '/pages/connectDetail/ea18/index',
    //   bluetoothPrefix: "BDE_WEIXIN_TTM",
    //   networkingMode: "WZLY",
    //   appIconUrl: "http://img.tmqyt.com/images/9612/15828138201330.png",
    //   isBind: false
    // }
  ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    utils.intiFindIndex();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let bindDeviceList = app.globalData.bindDeviceList;
    let deviceActionList = JSON.parse(JSON.stringify(this.data.deviceList));
    for (let i = 0; i < deviceActionList.length; i++) {
      for (let j = 0; j < bindDeviceList.length; j++) {
        if (Number(bindDeviceList[j].deviceType) === deviceActionList[i].appDeviceType) {
          deviceActionList[i].isBind = true;
          deviceActionList[i].deviceUUId = bindDeviceList[j].deviceUUId;
          deviceActionList[i].macAddr = bindDeviceList[j].macAddr;
        }
      }
    }
    this.setData({
      deviceList: deviceActionList
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  goToBindingOrConnect(e) {
    // 连接流程goToBindingOrConnect
    let idx = e.currentTarget.dataset.key
    let device = this.data.deviceList[idx]
    if (device.isBind) {
      wx.reLaunch({
        url: `${device.link}?name=` + encodeURIComponent(device.deviceName) + '&deviceId=' + encodeURIComponent(device.deviceUUId) + '&macAddr=' + encodeURIComponent(device.macAddr)
      });
    } else { // 绑定流程
      wx.reLaunch({
        url: '../scanDevices/scanDevices?deviceType=' + device.appDeviceType,
      })
    }
  }
})